package com.cn.xiongmeng.czlxz.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.cn.xiongmeng.czlxz.MainApplication;
import com.cn.xiongmeng.czlxz.R;


public class BannerFragment extends Fragment {
    private static final String TAG = "MainApplication:BannerFragment";
    private Activity activity;
    private View rootView;

    public BannerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach");

        activity = (Activity) context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_banner, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated");
    }
    @Override
    public void onStart() {
        super.onStart();
        // 启动动画
        Log.d(TAG, "onStart: ");
    }
    @Override
    public void onResume() {
        super.onResume();
        // 注册广播接收器
        Log.d(TAG, "onResume: ");
        loadBannerAd();
    }
    @Override
    public void onPause() {
        super.onPause();
        // 取消注册广播接收器
        Log.d(TAG, "onPause: ");
        MainApplication app = (MainApplication)(activity.getApplication());
        if (app.getBannerView() != null && app.getBannerView().getParent() != null) {
            ((ViewGroup) app.getBannerView().getParent()).removeView(app.getBannerView());
        }
    }
    @Override
    public void onStop() {
        super.onStop();
        // 停止动画
        Log.d(TAG, "onStop: ");
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 释放资源
        Log.d(TAG, "onDestroyView: ");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 释放资源
        Log.d(TAG, "onDestroy: ");
    }
    @Override
    public void onDetach() {
        super.onDetach();
        // 释放Activity的引用
        Log.d(TAG, "onDetach: ");
    }
    private void loadBannerAd(){
        MainApplication app = (MainApplication)(activity.getApplication());

        // 1、定义子视图（View）在 FrameLayout 中的布局特性（也就是，定义bannerView在FrameLayout 中的布局特性）
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        FrameLayout parentOfBannerView = rootView.findViewById(R.id.parent_of_banner_view);
        if (app.getBannerView() != null && app.getBannerView().getParent() == null) {
            parentOfBannerView.addView(app.getBannerView(), layoutParams);
        }
        if (app.getBannerView() != null){
            app.getBannerView().loadAd();
        }
    }
}